/*
 * @Description: 
 * @Author: your name
 * @version: 
 * @Date: 2025-10-27 11:53:02
 * @LastEditors: your name
 * @LastEditTime: 2025-10-27 14:25:51
 */
#include "wifi.h"
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_mac.h"
#include "lwip/err.h"
#include "lwip/sys.h"

/* The examples use WiFi configuration that you can set via project configuration menu.
   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
    例程使用的wifi配置你可以通过项目配置菜单来实现
    如果你不想这么搞，就修改下列定义
    比如#define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID       "ESP32"                   //wifi名称
#define EXAMPLE_ESP_WIFI_PASS       "123456789"               //wifi密码

static const char *TAG = "wifi softAP";                 //串口输出信息TAG

/*wifi事件处理函数*/
static void wifi_event_handler(void* arg, esp_event_base_t event_base, int32_t event_id, void* event_data)
{
    if (event_id == WIFI_EVENT_AP_STACONNECTED) {                          
        wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
        ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",MAC2STR(event->mac), event->aid);
    } 
    else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) 
    {                 
        wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
        ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",MAC2STR(event->mac), event->aid);
    }
}

/*wifi softap模式初始化*/
void wifi_init_softap(void)
{
    ESP_ERROR_CHECK(esp_netif_init());                  //初始化检查
    ESP_ERROR_CHECK(esp_event_loop_create_default());
    esp_netif_create_default_wifi_ap();

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();            //wifi相关设置初始化
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));

    ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,             //检查
                                                        ESP_EVENT_ANY_ID,
                                                        &wifi_event_handler,
                                                        NULL,
                                                        NULL));

    wifi_config_t wifi_config = {
        .ap = {
        .ssid = "ESP32_SoftAP",
        .password = "12345678",
        .channel = 6,  // 直接设置信道，比如6
        .max_connection = 4,
        .authmode = WIFI_AUTH_WPA_WPA2_PSK
        }
    };
    if (strlen(EXAMPLE_ESP_WIFI_PASS) == 0) {           //如果密码长度为0（未设置密码），则认证模式改为开放
        wifi_config.ap.authmode = WIFI_AUTH_OPEN;
    }

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));       //检查
    ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());
     //WiFi_AP初始化完成，串口输出信
    ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",wifi_config.ap.ssid, wifi_config.ap.password, wifi_config.ap.channel);
}
